/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package uky.article.imageviewer.views;

import java.io.File;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.skyfree.ghyll.tcard.core.ITWorkpiece;


/**
 * This ImageView class shows how to use SWTImageCanvas to 
 * manipulate images. 
 * <p>
 * To facilitate the usage, you should setFocus to the canvas
 * at the beginning, and call the dispose at the end.
 * <p>
 * @author Chengdong Li: cli4@uky.edu
 * @see uky.article.imageviewer.SWTImageCanvas
 */

public class ImageView extends ViewPart implements ISelectionListener, Listener
{
	class TreeLabelProvider extends LabelProvider {
		public String getText(Object element) 
		{
			File pic = (File)element;
			return pic.getName();
		}
		public Image getImage(Object element) {
			return IM_PIC;
		}
	}
	class TreeContentProvider implements IStructuredContentProvider, ITreeContentProvider 
	{
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object inputElement) 
		{
			return picDir.listFiles();
		}
		public Object[] getChildren(Object parentElement) 
		{
			return new Object[0];
		}
		public Object getParent(Object element) {
			return null;
		}
		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}
	}
	static Image IM_PIC = AbstractUIPlugin.imageDescriptorFromPlugin("org.skyfree.ghyll.image", "icons/Fit16.gif").createImage();
	
	
	public SWTImageCanvas imageCanvas;
	File picDir = null;


	private TreeViewer treeViewer;
	
	/**
	 * The constructor.
	 */
	public ImageView() {
	}
	
	/**
	 * Create the GUI.
	 * @param frame The Composite handle of parent
	 */
	public void createPartControl(Composite frame) {

		final SashForm sashForm = new SashForm(frame, SWT.NONE);

		this.treeViewer = new TreeViewer(sashForm, SWT.BORDER);
		treeViewer.setLabelProvider(new TreeLabelProvider());
		treeViewer.setContentProvider(new TreeContentProvider());
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener()
		{

			@Override
			public void selectionChanged(SelectionChangedEvent event) 
			{
				StructuredSelection select = (StructuredSelection)event.getSelection();
				File pic = (File) select.getFirstElement();
				if(pic!=null)imageCanvas.loadFitImage(pic.getAbsolutePath());			}
			
		});
		imageCanvas=new SWTImageCanvas(sashForm);
		this.imageCanvas.addListener(SWT.Resize,this);
		sashForm.setWeights(new int[] {141, 350 });
		initializeToolBar();
	}

	/**
	 * Called when we must grab focus.
	 * @see org.eclipse.ui.part.ViewPart#setFocus
	 */
	public void setFocus() {
		imageCanvas.setFocus();
	}

	/**
	 * Called when the View is to be disposed
	 */
	public void dispose() {
		imageCanvas.dispose();
		super.dispose();
	}
	private void initializeToolBar() {
		//IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
	}

	
	
	@Override
	public void init(IViewSite site) throws PartInitException 
	{
		super.init(site);
		site.getPage().addSelectionListener(this);
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) 
	{
		StructuredSelection ss = (StructuredSelection)selection;
		Object select = ss.getFirstElement();
		if(select instanceof ITWorkpiece)
		{
			this.picDir = new File(((ITWorkpiece)select).getURI(),"pics");
			this.treeViewer.setInput(this.picDir);
			if(this.treeViewer.getSelection().isEmpty())
			{
				this.imageCanvas.setImageData(null);
			}
		}
		else
		{
			this.treeViewer.setInput(null);
			this.imageCanvas.setImageData(null);
		}
		
	}

	@Override
	public void handleEvent(Event event) 
	{
		this.imageCanvas.fitCanvas();
		
	}
	
	public void DeleteSelectedPicture()
	{
		StructuredSelection ss = (StructuredSelection)this.treeViewer.getSelection();
		File select = (File) ss.getFirstElement();
		select.delete();
		this.treeViewer.remove(select);
		this.imageCanvas.setImageData(null);
		
	}
}